package com.brush.system.excelconvert;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.brush.common.annotation.DictDataProperty;
import com.brush.common.constant.CacheConstants;
import com.brush.common.core.domain.resp.SysDictDataQueryResp;
import org.springframework.data.redis.core.RedisTemplate;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 字段转换
 */
public class SysDictDataConverter implements Converter<String> {

    /**
     * Java类型
     *
     * @return
     */
    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    /**
     * Excel表格类型
     *
     * @return
     */
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 这里是读的时候会调用 不用管
     *
     * @return
     */
    @Override
    public String convertToJavaData(ReadConverterContext<?> context) {
        return context.getReadCellData().getStringValue();
    }


    /**
     * 这里是写的时候会调用(自己处理了)
     *
     * @param value               当前要写入的excel表中 java的值
     * @param contentProperty     JAVA实体类字段 @ExcelProperty(value = "创建时间").
     * @param globalConfiguration Global configuration.NotNull.
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {

        //获取JAVA字段
        Field field = contentProperty.getField();
        //获取JAVA字段上面的DictDataProperty注解
        DictDataProperty declaredAnnotation = field.getDeclaredAnnotation(DictDataProperty.class);
        //没有
        if (Objects.isNull(declaredAnnotation)) {
            return new WriteCellData<>(value);
        }

        String key = declaredAnnotation.value();
        //没有值
        if (StrUtil.isBlank(key)) {
            return new WriteCellData<>(value);
        }

        //不为空,从redis中查询
        String redisKey = CacheConstants.SYS_DICT_KEY + key;

        //从spring容器获取RedisTemplate ,不能使用 @Autowired
        RedisTemplate redisTemplate = SpringUtil.getBean("redisTemplate", RedisTemplate.class);

        List<SysDictDataQueryResp> list = (List<SysDictDataQueryResp>) redisTemplate.opsForValue().get(redisKey);

        if (Objects.isNull(list) || list.isEmpty()) {
            return new WriteCellData<>(value);
        }

        //查询值
        Optional<String> any = list.stream().filter(item -> item.getDictValue().equals(value))
                .map(SysDictDataQueryResp::getDictLabel)
                .findAny();

        //存在
        if (any.isPresent()) {
            return new WriteCellData<>(any.get());
        } else {
            return new WriteCellData<>(value);
        }
    }
}
